package com.yuejian_06.yuejian_06.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.bean.ClubBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.fragment.Constants_l;
import com.yuejian_06.yuejian_06.model.ClubModel;

public class ClubInfoActivity extends BaseActivity {
    TextView club_name,club_info,club_starttime,club_stoptime,club_address,club_tele;
    Button club_home,club_course,club_video;
    ImageView club_pic;
    String club_id,name,pic;
    ImageButton club_back,f_collected;
    String status;

    //判断收藏
    private LoginListener<SuccessBean> Listener2=new LoginListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean o) {
            SuccessBean mDataList=o;
            status=mDataList.getSuccess();
            if (status.equals("0"))
            {
                f_collected.setImageResource(R.drawable.collected_1);
            }
            else
            {
                f_collected.setImageResource(R.drawable.collected_2);
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"失败了",Toast.LENGTH_SHORT).show();
        }
    };
    //收藏／取消收藏
    private LoginListener<SuccessBean> Listener3=new LoginListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean o) {
            SuccessBean success=o;
            if (success.getSuccess().toString().equals("1"))
            {
                if (status.equals("0"))
                {
                    f_collected.setImageResource(R.drawable.collected_2);
                    Toast.makeText(ClubInfoActivity.this, "收藏成功", Toast.LENGTH_LONG).show();
                    status="1";
                }
                else
                {
                    f_collected.setImageResource(R.drawable.collected_1);
                    Toast.makeText(ClubInfoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                    status="0";
                }
            }
            else
            {
                Toast.makeText(ClubInfoActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"失败了",Toast.LENGTH_SHORT).show();
        }
    };


    @Override
    void initViews() {
        setLayout(R.layout.activity_club_info);
        club_name=(TextView)findViewById(R.id.club_info_name);
        club_info=(TextView)findViewById(R.id.club_info_info);
        club_starttime=(TextView)findViewById(R.id.club_info_starttime);
        club_stoptime=(TextView)findViewById(R.id.club_info_stoptime);
        club_address=(TextView)findViewById(R.id.club_info_address);
        club_tele=(TextView)findViewById(R.id.club_info_tele);
        club_pic=(ImageView)findViewById(R.id.club_info_pic);
        club_home=(Button)findViewById(R.id.club_info_btn1);
        club_course=(Button)findViewById(R.id.club_info_btn2);
        club_video=(Button)findViewById(R.id.club_info_btn3);
        club_back=(ImageButton)findViewById(R.id.club_back);
        f_collected=(ImageButton)findViewById(R.id.f_collected);
        Bundle bundle = getIntent().getExtras();
        club_id = bundle.getString("club_id");

        ClubModel clubModel=new ClubModel();
        clubModel.clubInfo(club_id,Listener);
        //判断是否收藏
        ClubModel clubModel2=new ClubModel();
        clubModel2.isCollect(getUser_id(),club_id,Listener2);
    }

    @Override
    void initEvents() {
        club_course.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Intent intent=new Intent(ClubInfoActivity.this, ClubCourseActivity.class);
                intent.putExtra("club_id",club_id);
                intent.putExtra("club_name",name);
                intent.putExtra("club_pic",pic);
                startActivity(intent);
            }
        });

        club_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Intent intent=new Intent(ClubInfoActivity.this, ClubVideoActivity.class);
                intent.putExtra("club_id",club_id);
                intent.putExtra("club_name",name);
                intent.putExtra("club_pic",pic);
                startActivity(intent);
            }
        });

        club_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        f_collected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ClubModel clubModel3=new ClubModel();
                clubModel3.CollectClub(getUser_id(),club_id,Listener3);
            }
        });

    }

    @Override
    void initData() {

    }



    private LoginListener<ClubBean> Listener=new LoginListener<ClubBean>() {
        @Override
        public void onResponse(ClubBean o) {
            ClubBean mDataList=o;
            name=mDataList.getClub_name();
            pic=mDataList.getClub_pic();
            club_name.setText(mDataList.getClub_name().toString());
            club_info.setText(mDataList.getClub_intro().toString());
            club_tele.setText(mDataList.getClub_phone().toString());
            club_address.setText(mDataList.getClub_address().toString());
            club_starttime.setText(mDataList.getStarttime().toString());
            club_stoptime.setText(mDataList.getStoptime().toString());

            String internetUrl = Constants.SSS+"yuejian/Public/clubpics/"+mDataList.getClub_pic().toString();
            if(!mDataList.getClub_pic().isEmpty())
            {
                Picasso
                        .with(ClubInfoActivity.this)
                        .load(internetUrl)
                        .into(club_pic);}
            else
            {
                club_pic.setImageResource(R.drawable.club_wu);
            }

        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"失败了"+club_id,Toast.LENGTH_SHORT).show();

        }
    };
}
